/**
 * 
 * 全局人事报表设置
 * 
 */

import mockData from './mock';

import {hrAjax as proFetch} from 'src/hrpub/common/utils/utils';

import commonModal from '../../store/model';

export default {
    name: 'gloRep',
    data: {
        ...commonModal.data
    },
    sync: {
        update: (state, payload) => {
            return {
                ...state,
                ...payload
            }
        }
    },
    async: {
        // 获取左树的数据
        getTreeData(dispatch, getState, payload) {
            const state = getState()['gloRep'];

            return proFetch({
                url: '/nccloud/hrhi/rptfef/FolderQueryAction.do',
                body: {
                    // pk_org: state.context.pk_org
                }
            })
                .then((res) => {
                    mockData.rootTreeData.name = state.language['hi6007-000199']; // 报表目录
                    mockData.rootTreeData.refname = state.language['hi6007-000199']; // 报表目录

                    if(res.success) {
                         const treeData = [{
                            ...mockData.rootTreeData,
                            id: res.data[0].pid,
                            key: res.data[0].pid,
                            refpk: res.data[0].pid,
                            children: res.data
                        }];
                        // 设置节点数据
                        payload.syncTree.setSyncTreeData('reportTree', treeData);
                        // 默认展开根结点
                        payload.syncTree.openNodeByPk('reportTree', 'root');
                        // 全局和根节点不能编辑
                        payload.syncTree.hideIcon('reportTree', 'root', {
                            delIcon: false,
                            editIcon: false,
                            addIcon: false
                        });
                        payload.syncTree.hideIcon('reportTree', '1001A010000000000OSX', {
                            delIcon: false,
                            editIcon: false,
                            addIcon: false
                        });
                        dispatch({
                            type: 'gloRep/update',
                            payload: {
                                treeData: treeData
                            }
                        });
                    }
                });
        },

        // 获取表格数据
        getTableData(dispatch, getState, {editTable}) {
            const state = getState()['gloRep'];
            return proFetch({
                url: '/nccloud/hrhi/rptfef/ReportQueryAction.do',
                body: {
                    // pk_org: state.context.pk_org,
                    pk_folder: state.pk_folder
                }
            })
                .then((res) => {
                    if(res.success) {
                        const tableData = res.data ? res.data.rptlist : {rows: []};

                        editTable.setTableData('rptlist', tableData, false);
                        dispatch({
                            type: 'gloRep/update',
                            payload: {
                                tableData: tableData,
                                selectedRow: {
                                    ...tableData.rows[0],
                                    index: 0
                                }
                            }
                        });
                    }
                    return res;
                });
        },
        // 提交新增弹窗数据
        submitReport(dispatch, getState, payload) {
            let state = getState()['gloRep'];

            let postData = {
                model: payload.formData,
                // pk_org: state.context.pk_org
            };

            let url = '/nccloud/hrhi/rptfef/ReportAddSaveAction.do';

            if(state.buttonClick === 'paste') {
                url = '/nccloud/hrhi/rptfef/ReportPasteSaveAction.do';
            }

            return proFetch({
                url: url,
                body: postData
            });
        },
        // 删除一条左树数据
        deleteReport(dispatch, getState, payload) {
            let state = getState()['gloRep'];

            let {selectedRow} = state;

            let postData = {
                // pk_org: state.context.pk_org,
                pk_rpt_def: selectedRow.values['pk_rpt_def'].value
            };

            return proFetch({
                url: '/nccloud/hrhi/rptfef/ReportDeleteAction.do', 
                body: postData
            });
        },
        // 新增/修改左树弹窗模版数据获取
        getEditTreeNodeModalTemplate(dispatch, getState, payload) {
            let state = getState()['gloRep'];

            let postData = {
                pk_folder: payload.pk_folder,
                // pk_org: state.context.pk_org
            }

            let url = '/nccloud/hrhi/rptfef/FolderNewAction.do';

            if(payload.clickType === 'change') {
                url = '/nccloud/hrhi/rptfef/FolderEditAction.do';
            }

            return proFetch({
                url: url,
                body: postData
            });
        },
        // 删除左树节点
        deleteTreeNode(dispatch, getState, payload) {
            let state = getState()['gloRep'];

            let postData = {
                pk_folder: payload.pk_folder,
                // pk_org: state.context.pk_org
            };

            return proFetch({
                url: '/nccloud/hrhi/rptfef/FolderDelAction.do',
                body: postData
            });
        },
        // 保存表格修改
        saveTableChange(dispatch, getState, payload) {
            let state = getState()['gloRep'];

            let postData = {
                model: payload.tableData,
                // pk_org: state.context.pk_org
            };

            return proFetch({
                url: '/nccloud/hrhi/rptfef/ReportEditSaveAction.do',
                body: postData
            });
        },
        // 保存新增/修改左树节点
        submitTreeNode(dispatch, getState, payload) {
            let state = getState()['gloRep'];
            let postData = {
                model: payload.formData,
                // pk_org: state.context.pk_org
            };

            return proFetch({
                url: '/nccloud/hrhi/rptfef/FolderSaveAction.do',
                body: postData
            });
        },
        // 检测表格数据是否能修改
        checkTableEdit(dispatch, getState, payload) {
            let state = getState()['gloRep'];
            let postData = {
                // pk_org: state.context.pk_org,
                pk_rpt_def: payload.pk_rpt_def
            };

            return proFetch({
                url: '/nccloud/hrhi/rptfef/ReportEditAction.do',
                body: postData
            });
        },
        // 获取复制初始化数据
        getCopyData(dispatch, getState, payload) {
            let state = getState()['gloRep'];
            let postData = {
                pk_folder: state.pk_folder,
                // pk_org: state.context.pk_org,
                pk_rpt_def: payload.pk_rpt_def.value
            };

            return proFetch({
                url: '/nccloud/hrhi/rptfef/ReportPasteAction.do',
                body: postData
            });
        },
        // 获取花名册设计初始化数据
        getDesignInitData(dispatch, getState, payload) {
            let state = getState()['gloRep'];

            let postData = {
                // pk_org: state.context.pk_org,
                pk_rpt_def: state.selectedRow.values['pk_rpt_def'].value
            };

            return proFetch({
                url: '/nccloud/hrhi/rptfef/RptListDesignInitAction.do',
                body: postData
            });
        },
        // 改变信息集
        changeInfoGroup(dispatch, getState, payload) {
            let state = getState()['gloRep'];

            let postData = {
                pk_infoset: payload.pk_infoset,
                // pk_org: state.context.pk_org
            };

            return proFetch({
                url: '/nccloud/hrhi/rptfef/RptListDesignSelSetAction.do',
                body: postData
            });
        },
        // 提交穿梭框表单
        submitDesignModal(dispatch, getState, payload) {
            let state = getState()['gloRep'];
            state.transferSortList.map((item, index) => {
                delete item['suffix'];
                delete item['prefix'];

                if(!('is_asc' in item)) {
                    item['is_asc'] = 'Y'
                }
                item['sortorder'] = index + 1;
            });
            let postData = {
                // pk_org: state.context.pk_org,
                pk_rpt_def: state.selectedRow.values['pk_rpt_def'].value,
                rightList: JSON.stringify(state.transferList2),
                sortList: JSON.stringify(state.transferSortList)
            };

            return proFetch({
                url: '/nccloud/hrhi/rptfef/RptListDesignDoAction.do',
                body: postData
            });
        },
        // 点击新增初始化新增弹窗数据
        initAddModal(dispatch, getState, payload) {
            let state = getState()['gloRep'];
            let postData = {
                pk_folder: state.pk_folder,
                // pk_org: state.context.pk_org
            };

            return proFetch({
                url: '/nccloud/hrhi/rptfef/ReportAddAction.do',
                body: postData
            });
        },
        // 点击设计检查那啥啥
        checkDesignModal(dispatch, getState, payload) {
            let state = getState()['gloRep'];

            let postData = {
                // pk_org: state.context.pk_org,
                pk_rpt_def: state.selectedRow.values['pk_rpt_def'].value
            };

            return proFetch({
                url: '/nccloud/hrhi/rptfef/ReportDesignAction.do',
                body: postData
            });
        },
        // 预览
        preview(dispatch, getState, payload) {
            let state = getState()['gloRep'];

            let postData = {
                // pk_org: state.context.pk_org,
                pk_rpt_def: state.selectedRow.values['pk_rpt_def'].value
            };

            return proFetch({
                url: '/nccloud/hrhi/rptfef/ReportPreviewAction.do',
                body: postData
            });
        },
        // 权限分配
        rightAssignment(dispatch, getState, payload) {
            let state = getState()['gloRep'];

            let postData = {
                // pk_org: state.context.pk_org,
                pk_rpt_def: state.selectedRow.values['pk_rpt_def'].value
            };
            
            return proFetch({
                url: '/nccloud/hrhi/rptfef/ReportAuthAction.do',
                body: postData
            });
        },
        wpsDesignData(dispatch, getState, payload) {
            let state = getState()['gloRep'];

            let postData = {
                pk_rpt_def: state.selectedRow.values['pk_rpt_def'].value
            };

            return proFetch({
                url: '/nccloud/hrpub/rptdef/DesignAction.do',
                body: postData
            });
        },
        wpsPreStatus(dispatch, getState, payload) {
            return proFetch({
                url: '/nccloud/hrpub/rptdef/PreAction.do',
                body: payload
            });
        }
    }
};